package thinker.miracle.utils;

/**
 * @author bo.chen
 *
 */
public class StringUtil {

	/**
	 * 将以逗号分隔的字符串转换成字符串数组
	 * 
	 * @param valStr
	 * @return String[]
	 */
	public static String[] str2Array(String valStr){
	    int i = 0;
	    String TempStr = valStr;
	    String[] returnStr = new String[valStr.length() + 1 - TempStr.replace(",", "").length()];
	    valStr = valStr + ",";
	    while (valStr.indexOf(',') > 0) {
	        returnStr[i] = valStr.substring(0, valStr.indexOf(','));
	        valStr = valStr.substring(valStr.indexOf(',')+1 , valStr.length());
	        i++;
	    }
	    return returnStr;
	}
	
	/**
	 * 获取字符串编码
	 * 
	 * @param str
	 * @return
	 */
	public static String getEncoding(String str) {      
	       String encode = "GB2312";      
	      try {      
	          if (str.equals(new String(str.getBytes(encode), encode))) {      
	               String s = encode;      
	              return s;      
	           }      
	       } catch (Exception exception) {      
	       }      
	       encode = "ISO-8859-1";      
	      try {      
	          if (str.equals(new String(str.getBytes(encode), encode))) {      
	               String s1 = encode;      
	              return s1;      
	           }      
	       } catch (Exception exception1) {      
	       }      
	       encode = "UTF-8";      
	      try {      
	          if (str.equals(new String(str.getBytes(encode), encode))) {      
	               String s2 = encode;      
	              return s2;      
	           }      
	       } catch (Exception exception2) {      
	       }      
	       encode = "GBK";
	      try {      
	          if (str.equals(new String(str.getBytes(encode), encode))) {      
	               String s3 = encode;      
	              return s3;      
	           }      
	       } catch (Exception exception3) {      
	       }      
	      return "";      
	   } 
	
	
	/**
	 * 转换成string
	 * 
	 * @param o
	 * @return
	 */
	public static String getString(Object o){
		return o == null ? null : o.toString();
	}
	
	
	/**
	 * 补 0
	 * 将字符串补充位数，不足在前面补0
	 * @param str	字符串
	 * @param b		位数
	 * @return
	 */
	public static String supplementZero(String str,int b){
		if(str != null && str.length() < b){
			char[] cArr = new char[b];
			char[] strArr = str.toCharArray();
			for(int i = cArr.length-1,j = strArr.length-1;i>=0;i--,j--){
				if(j>=0){
					cArr[i] = strArr[j];
				}else{
					cArr[i] = '0';
				}
			}
			str = String.valueOf(cArr);
		}
		return str;
	}
	
	/**
	 * 补 0
	 * 将字符串补充位数，不足在前面补0
	 * @param a		被补位的数字
	 * @param b		位数
	 * @return
	 */
	public static String supplementZero(int a,int b){
		String str = String.valueOf(a);
		if(str != null && str.length() < b){
			char[] cArr = new char[b];
			char[] strArr = str.toCharArray();
			for(int i = cArr.length-1,j = strArr.length-1;i>=0;i--,j--){
				if(j>=0){
					cArr[i] = strArr[j];
				}else{
					cArr[i] = '0';
				}
			}
			str = String.valueOf(cArr);
		}
		return str;
	}
	
	/**
	 * 判断字符串数组是否包含一个字符串
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static boolean isContent(String[] a,String b){
		boolean flag = false;
		if(a == null){
			return flag;
		}
		for(String s : a){
			if(s == b){
				flag = true;
			}else if(s != null && b != null && s.equals(b)){
				flag = true;
			}
		}
		return flag;
	}
	
	/**
	 * 替换字符
	 * 
	 * @param str
	 * @param oldS
	 * @param newS
	 * @return
	 * @throws Exception
	 */
	public static String replaceAll(String str,String oldS,String newS) throws Exception{
		if(str == null || oldS == null || newS == null){
			throw new Exception("str can not be null");
		}
		str = str.replace(oldS, newS);
		
		if(str.indexOf(oldS) >= 0){
			str = replaceAll(str,oldS,newS);
		}
		return str;
	}
}
